#****************************************************************************
#
# Makefile for StarCore.
# www.srplab.com
#****************************************************************************
DEBUG          := NO
PROFILE        := NO
#****************************************************************************
CC     := gcc
CXX    := g++
LD     := g++
AR     := ar
RANLIB := ranlib

DEBUG_CFLAGS     := -Wall -Wno-format -g -DDEBUG -DENV_LINUX
RELEASE_CFLAGS   := -Wall -Wno-unknown-pragmas -Wno-format -O3 -DENV_LINUX

LIBS	:= -ldl -lpthread -lrt

DEBUG_CXXFLAGS   := ${DEBUG_CFLAGS} 
RELEASE_CXXFLAGS := ${RELEASE_CFLAGS}

DEBUG_LDFLAGS    := -g
RELEASE_LDFLAGS  :=

ifeq (YES, ${DEBUG})
   CFLAGS       := ${DEBUG_CFLAGS}
   CXXFLAGS     := ${DEBUG_CXXFLAGS}
   LDFLAGS      := ${DEBUG_LDFLAGS}
else
   CFLAGS       := ${RELEASE_CFLAGS}
   CXXFLAGS     := ${RELEASE_CXXFLAGS}
   LDFLAGS      := ${RELEASE_LDFLAGS}
endif

ifeq (YES, ${PROFILE})
   CFLAGS   := ${CFLAGS} -pg -O3
   CXXFLAGS := ${CXXFLAGS} -pg -O3
   LDFLAGS  := ${LDFLAGS} -pg
endif

#****************************************************************************
# Makefile code common to all platforms
#****************************************************************************

CFLAGS   := ${CFLAGS}   ${DEFS}
CXXFLAGS := ${CXXFLAGS} ${DEFS}

#****************************************************************************
# include source and paths
#****************************************************************************

INCS_T := JPGLib/JPEGLIB2 PNGLib/lpng1212 GIFLib
#INCS_T += ../source/script_layer/lua/src
INCS   = $(addprefix -I,$(INCS_T))

JPEGLIB_SRCS_NOTNEEDED :=
JPEGLIB_CXXSRCS += $(filter-out $(addprefix JPGLib/JPEGLIB2/,$(JPEGLIB_SRCS_NOTNEEDED)),$(wildcard JPGLib/JPEGLIB2/*.cpp))
JPEGLIB_CSRCS += $(filter-out $(addprefix JPGLib/JPEGLIB2/,$(JPEGLIB_SRCS_NOTNEEDED)),$(wildcard JPGLib/JPEGLIB2/*.C))

PNGLIB_SRCS_NOTNEEDED := example.c pngtest.c
PNGLIB_CXXSRCS += $(filter-out $(addprefix PNGLib/lpng1212/,$(PNGLIB_SRCS_NOTNEEDED)),$(wildcard PNGLib/lpng1212/*.cpp))
PNGLIB_CSRCS += $(filter-out $(addprefix PNGLib/lpng1212/,$(PNGLIB_SRCS_NOTNEEDED)),$(wildcard PNGLib/lpng1212/*.c))
PNGLIB_CSRCS += $(wildcard PNGLib/ZLib/*.c)

GIFLIB_CXXSRCS +=
GIFLIB_CSRCS += GIFLib/readGIF.c GIFLib/writeGIF.c

#****************************************************************************
JPEGLIB_CXXOBJS := $(JPEGLIB_CXXSRCS:%.cpp=%.o)
JPEGLIB_COBJS   := $(JPEGLIB_CSRCS:%.C=%.o)

PNGLIB_CXXOBJS := $(PNGLIB_CXXSRCS:%.cpp=%.o)
PNGLIB_COBJS   := $(PNGLIB_CSRCS:%.c=%.o)

GIFLIB_CXXOBJS := $(GIFLIB_CXXSRCS:%.cpp=%.o)
GIFLIB_COBJS   := $(GIFLIB_CSRCS:%.c=%.o)

#****************************************************************************
CXXOBJS := ${JPEGLIB_CXXOBJS} ${PNGLIB_CXXOBJS} ${GIFLIB_CXXOBJS}
COBJS := ${JPEGLIB_COBJS} ${PNGLIB_COBJS} ${GIFLIB_COBJS}

LIB_JPEGLIB_OBJS := ${JPEGLIB_CXXOBJS} ${JPEGLIB_COBJS} 
LIB_PNGLIB_OBJS := ${PNGLIB_CXXOBJS} ${PNGLIB_COBJS}
LIB_GIFLIB_OBJS := ${GIFLIB_CXXOBJS} ${GIFLIB_COBJS}

#****************************************************************************
# Targets of the build
#****************************************************************************
OBJS_PATH = .

LIB_JPEGLIB := ${OBJS_PATH}/jpeglib2.a
LIB_PNGLIB := ${OBJS_PATH}/libpng.a
LIB_GIFLIB := ${OBJS_PATH}/giflib.a

all: ${LIB_JPEGLIB} ${LIB_PNGLIB} ${LIB_GIFLIB}

#****************************************************************************
# Output
#****************************************************************************

${LIB_JPEGLIB} : ${LIB_JPEGLIB_OBJS}	
	${AR} r $@ ${LIB_JPEGLIB_OBJS}
	${RANLIB} $@

${LIB_PNGLIB} : ${LIB_PNGLIB_OBJS}	
	${AR} r $@ ${LIB_PNGLIB_OBJS}
	${RANLIB} $@

${LIB_GIFLIB} : ${LIB_GIFLIB_OBJS}	
	${AR} r $@ ${LIB_GIFLIB_OBJS}
	${RANLIB} $@
#****************************************************************************
# common rules
#****************************************************************************

${CXXOBJS} :
	${CXX} -fPIC ${CXXFLAGS} ${INCS} $< -o $@ -c $*.cpp

${COBJS} : 
	${CC} -fPIC ${CFLAGS} ${INCS} -o $@ -c $*.c

clean:
	-rm -f core ${CXXOBJS} ${COBJS} ${LIB_JPEGLIB} ${LIB_PNGLIB} ${LIB_GIFLIB}

    

